package com.crb.crm.manage.web.biz.log;

import com.crb.crm.manage.entity.biz.CrmReqLogSearchDTO;
import com.crb.crm.manage.pojo.User;
import com.crb.crm.manage.service.biz.CrmReqLogService;
import com.crb.crm.manage.utils.PageDataResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author huangjian
 */
@Controller
@RequestMapping("/crmReqLog")
public class CrmReqLogController {
	
	private static final Logger logger = LoggerFactory.getLogger(CrmReqLogController.class);
	@Autowired
	private CrmReqLogService service;
	@RequestMapping("/crmReqLogList")
	public String crmReqLogList() {
		return "biz/log/crmReqLogList";
	}
	
	/**
	 * 分页查询商户列表
	 * @return ok/fail
	 */
	@RequestMapping(value = "/getCrmReqLogs", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions(value="getCrmReqLogs")
	public PageDataResult getCrmReqLogs(@RequestParam("page") Integer page,
                                   @RequestParam("limit") Integer limit, CrmReqLogSearchDTO searchDTO) {
		logger.debug("分页查询列表！搜索条件：searchDTO：" + searchDTO + ",page:" + page+ ",每页记录数量limit:" + limit);
		PageDataResult pdr = new PageDataResult();
		try {
			if (null == page) {
				page = 1;
			}
			if (null == limit) {
				limit = 10;
			}
			// 获取商户
			pdr = service.getCrmReqLogs(searchDTO, page, limit);
			logger.debug("列表查询=pdr:" + pdr);

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("列表查询异常！", e);
		}
		return pdr;
	}

	/**
	 * 删除
	 * @return ok/fail
	 */
	@RequestMapping(value = "/delCrmReqLog", method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("delCrmReqLog")
	public String delCrmReqLog(@RequestParam("id") String id) {
		logger.debug("删除crmReqLog！id:" + id);
		String msg = "";
		try {
			if (null == id) {
				logger.debug("删除crmReqLog，结果=请求参数有误，请您稍后再试");
				return "请求参数有误，请您稍后再试";
			}
			User existUser = (User) SecurityUtils.getSubject().getPrincipal();
			if (null == existUser) {
				logger.debug("删除crmReqLog，结果=您未登录或登录超时，请您登录后再试");
				return "您未登录或登录超时，请您登录后再试";
			}
			// 删除商户
			msg = service.delCrmReqLog(id);
			logger.info("删除crmReqLog:" + msg + "。userId=" + id + "，操作用户id:"+ existUser.getId());
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("删除restLog异常！", e);
			msg = "操作异常，请您稍后再试";
		}
		return msg;
	}

	/**
	 * 查看日志内容
	 * @return ok/fail
	 */
	@RequestMapping(value = "/showLogs", method = RequestMethod.GET)
	@ResponseBody
	@RequiresPermissions({"showCrmReqData","showCrmContent"})
	public Map<String, Object> showLogs(@RequestParam("logType") String logType, @RequestParam("id") String id) {
		logger.debug("查看日志内容！id=" + id+",logType="+logType);
		Map<String, Object> map = new HashMap<>();
		try {
			if (null == id) {
				logger.debug("查看日志内容==请求参数有误，请您稍后再试");
				map.put("msg", "请求参数有误，请您稍后再试");
				return map;
			}
			// 查询商户
			String logContent = service.getLog(id,logType);
			logger.debug("查看日志内容！logContent=" + logContent);
			if (StringUtils.isNotBlank(logContent)) {
				map.put("logContent", logContent);
				map.put("msg", "ok");
			} else {
				map.put("msg", "查看日志内容有误，请您稍后再试");
			}
			logger.debug("查看日志内容数据成功！map=" + map);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
			map.put("msg", "查看日志内容错误，请您稍后再试");
			logger.error("查看日志内容数据异常！", e);
		}
		return map;
	}
}
